package com.infore.dataReceiving.export.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.infore.dataReceiving.export.service.IExportService;
import com.infore.dataReceiving.export.util.ExportUtil;
import com.infore.dataReceiving.externalInterface.siteManage.ISiteManageService;
import com.infore.dataReceiving.select.dto.QueryParamDto;
import com.infore.dataReceiving.service.IInstructionsService;
import com.infore.dataReceiving.util.ArrayUtils;
import com.infore.dataReceiving.util.JsonUtils;
import com.infore.dataReceiving.util.ObjectMapperUtil;
import com.infore.dataReceiving.util.ResultEntity;

@Service
public class ExportServiceImpl implements IExportService {

	@Autowired
	IInstructionsService iInstructionsService;
	
	@Autowired
	ISiteManageService iSiteManageService;
	
	@Autowired
	ObjectMapperUtil ObjectMapperUtil;

	@SuppressWarnings("unchecked")
	@Override
	public byte[] exportExcelData(HttpServletRequest request) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询数据的参数
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String indexCodes = paramsJson.getString("indexCodes");
		String stationCodes = paramsJson.getString("stationCodes");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		List<String> indexList = ArrayUtils.stringToList(indexCodes);
		List<Map<String, Object>> historyList = iInstructionsService.queryRowToColHourData(stationCodes,startTime,endTime,"1","99999");
		QueryParamDto queryParamDto = JsonUtils.readJsonFromClassPath("/queryParam/siteQuery.json", QueryParamDto.class);
		ResultEntity resultEntity = iSiteManageService.queryStation(queryParamDto);
		Map<String, Object> dataMap = (Map<String, Object>)resultEntity.getContent();
		List<Map<String, Object>> siteList = (List<Map<String, Object>>)dataMap.get("dataList");
		Map<String, Map<String, Object>> siteMap = new HashMap<>();
		for(Map<String, Object> map:siteList) {
			siteMap.put(map.get("stationMn").toString(), map);
		}
		List<Map<String, Object>> list = new ArrayList<>();
		SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (Map<String, Object> map : historyList) {
			Map<String, Object> historyMap = new HashMap<>();
			String mns = map.get("mn").toString();
			historyMap.put("stationName", siteMap.get(mns).get("stationName").toString());
//			historyMap.put("province", siteMap.get(mns).get("cityName")==null?null:siteMap.get(mns).get("cityName").toString());
			historyMap.put("province", "重庆市");
			historyMap.put("basin", siteMap.get(mns).get("basinn_basinnName")==null?null:siteMap.get(mns).get("basinn_basinnName").toString());
			historyMap.put("mn", map.get("mn")==null?null:map.get("mn").toString());
			historyMap.put("dataTime", sdfDateFormat.format((Date)map.get("dataTime")));
			for (String indexCode : indexList) {
				historyMap.put(indexCode, map.get(indexCode)==null?null:map.get(indexCode).toString());
			}
//			historyMap.put("w01010", map.get("w01010")==null?null:map.get("w01010").toString());
//			historyMap.put("w01001", map.get("w01001")==null?null:map.get("w01001").toString());
//			historyMap.put("w01009", map.get("w01009")==null?null:map.get("w01009").toString());
//			historyMap.put("w01003", map.get("w01003")==null?null:map.get("w01003").toString());
//			historyMap.put("w01014", map.get("w01014")==null?null:map.get("w01014").toString());
//			historyMap.put("w01019", map.get("w01019")==null?null:map.get("w01019").toString());
//			historyMap.put("w21003", map.get("w21003")==null?null:map.get("w21003").toString());
//			historyMap.put("w21011", map.get("w21011")==null?null:map.get("w21011").toString());
//			historyMap.put("w21001", map.get("w21001")==null?null:map.get("w21001").toString());
//			historyMap.put("w01018", map.get("w01018")==null?null:map.get("w01018").toString());
//			historyMap.put("w01016", map.get("w01016")==null?null:map.get("w01016").toString());
			list.add(historyMap);
		}
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

}
